{extend name="public:head"}
{block name="main"}

<form class="layui-form" style="margin: 20px">
    <div class="demoTable">
        条件搜索：

        <div class="layui-input-inline" style="width: 120px">
            <select name="signData" id="signData" lay-filter="from_name">
                <option></option>
            </select>
        </div>

        <div class="layui-inline">
            <select name="status">
                <option value="">全部状态</option>
                <option value="1">正常</option>
                <option value="0">禁用</option>
            </select>
        </div>

        <div class="layui-inline">
            <input class="layui-input" name="channel"  autocomplete="off" placeholder="渠道名称">
        </div>

        <div class="layui-inline">
            <button class="layui-btn" lay-filter="submit" lay-submit>搜索</button>
            <button class="layui-btn layui-btn-primary"  lay-filter="submit" lay-submit>重置</button>
        </div>

    </div>
</form>

<table class="layui-hide" id="all" lay-filter="all" lay-data="{id: 'sousuo'}"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>

<script type="text/html" id="host_d"></script>

<script type="text/html" id="host_f"></script>

<script type="text/html" id="status">
    {{#  if(d.status == '1'){ }} <a class="layui-btn layui-btn-xs">启用</a> {{#  } }}
    {{#  if(d.status == '0'){ }} <a class="layui-btn layui-btn-xs layui-btn-danger">禁用</a> {{#  } }}
    {{#  if(d.repeat == '1'){ }} <a class="layui-btn layui-btn-xs">重复</a> {{#  } }}
    {{#  if(d.repeat == '0'){ }} <a class="layui-btn layui-btn-xs layui-btn-danger">不重复</a> {{#  } }}
</script>

<script>
    layui.use(['table','layer','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var token = layui.data('cache').token;
        var a = false;
        table.render({
            elem: '#all'
            ,url:'/login/Sign/allSignApi'
            ,headers:{"token":token}
            ,toolbar: '#toolbarDemo'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'sign_id', title:'ID',  fixed: 'left', unresize: true, sort: true}
                ,{field:'channel', title:'渠道名称'}
                ,{field:'sign', title:'标识码'}
                ,{field:'pwd', title:'密码'}
                ,{field:'status', title:'状态', templet:'#status',event:"status"}
                ,{field:'host_d', title:'单期域名' ,templet:'#host_d'}
                ,{field:'host_f', title:'分期域名' ,templet:'#host_f'}
                ,{field:'add_time', title:'添加时间'}
                ,{field:'update_time', title:'修改时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });
        // 表格重载 用于搜索
        form.on('submit(submit)', function(data){
            var field=data.field;
            if($(data.elem).html()=="重置"){
                $("input").val("");
                location.reload();
            }
            table.reload('all', {
                url: '/admin/Sign/findSignApi',
                where: {field}
            });
            return false;
        });

        table.on('tool(all)', function(obj){
            var data = obj.data; //获得当前行数据
            var id = obj.data.sign_id
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            // 编辑账号
            if(obj.event == 'edit'){
                window.location.href= ("/login/sign/updateSign/id/"+id+"/token/" + token);
            }

            if(obj.event == 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        url:"/login/Sign/delSignApi",
                        type:"get",
                        headers:{"token":token},
                        data:{"id":id},
                        dataType:"json",
                        success:function (res) {
                            if(res.code == 200){
                                layer.msg('删除成功！！！');
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                        },
                        error:function (error) {
                            layer.msg('删除失败！！！');
                        }
                    })
                });
            }
        });
        $('#all').on('click', function(){
            layer.msg('hello');
        });
    });
</script>

{/block}